function OUTPOS = subplot_outpos(DIM,HEADER,FOOTER,PAD)

FIGH = (1 - HEADER - FOOTER - DIM(1)*PAD)/DIM(1);
FIGW = (1 - DIM(2)*PAD)/DIM(2);

VSPACE = zeros([(3+2*DIM(1)) 1]);
VSPACE(1) = HEADER;
VSPACE([2 (end-1)]) = PAD/2;
VSPACE(end) = FOOTER;
VSPACE(3:2:(end-2)) = FIGH;
VSPACE(4:2:(end-2)) = PAD;

VSPACE = cumsum(flip(VSPACE));
VSPACE = VSPACE(3:2:(end-2)) - FIGH;
VSPACE = flip(VSPACE);

HSPACE = zeros([(1+2*DIM(2)) 1]);
HSPACE([1 end]) = PAD/2;
HSPACE(2:2:(end-1)) = FIGW;
HSPACE(3:2:(end-1)) = PAD;

HSPACE = cumsum(HSPACE);
HSPACE = HSPACE(2:2:(end-1)) - FIGW;

OUTPOS = cell(DIM);
for i = 1:numel(OUTPOS)
    [COL,ROW] = ind2sub(flip(DIM),i);
    OUTPOS{ROW,COL} = [HSPACE(COL) VSPACE(ROW) FIGW FIGH];
end
                    


